package com.code.petapp.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.ViewTreeObserver;
import android.widget.Toast;

import com.code.petapp.bean.UserBean;
import com.code.petapp.databinding.ActivityWelcomeBinding;
import com.code.petapp.http.HttpListener;
import com.code.petapp.util.SPCommonUtils;
import com.code.petapp.util.UserUtils;

import io.reactivex.rxjava3.annotations.Nullable;

public class WelcomeActivity extends BaseActivity {
    ActivityWelcomeBinding binding;
    private boolean isAppReady = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityWelcomeBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        binding.main.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                if (isAppReady) {
                    binding.main.getViewTreeObserver().removeOnPreDrawListener(this);
                }
                return isAppReady;
            }
        });
        boolean isLogin = SPCommonUtils.getInstance().getIsLogin();
        if (isLogin) {//登录状态如果已登录，无需登录，直接跳转到主页
            UserBean userBean = new UserBean();
            userBean.setUsername(SPCommonUtils.getInstance().getUserName());
            userBean.setPassword(SPCommonUtils.getInstance().getPassword());
            UserUtils.login(userBean, new HttpListener<>() {
                @Override
                public void success(@Nullable UserBean baseBean) {
                    isAppReady = true;
                    SPCommonUtils.getInstance().setIsLogin(true);
                    SPCommonUtils.getInstance().setToken(baseBean.getToken());
                    SPCommonUtils.getInstance().setPhone(baseBean.getPhone());
                    SPCommonUtils.getInstance().setEmail(baseBean.getEmail());
                    Intent intent = new Intent(WelcomeActivity.this, MainActivity.class);
                    startActivity(intent);
                    finish();
                }

                @Override
                public void error(int code, @Nullable String error) {
                    if (code == 5003) {
                        SPCommonUtils.getInstance().Clear();
                        startActivity(new Intent(WelcomeActivity.this, LoginActivity.class));
                        toastShortShow("登录信息已过期，请重新登录");
                        finish();
                    } else {
                        toastShortShow(error);
                    }

                }
            });
        } else {
            new Handler(Looper.getMainLooper()).postDelayed(() -> {
                isAppReady = true;
                Intent intent = new Intent(WelcomeActivity.this, StartActivity.class);
                startActivity(intent);
                finish();
            }, 1000);
        }
    }
}